<?php

require_once("../pdo/lectures.php");

class LecturesTest
{
	public $Lectures;
	
	public function test_get_by_instance($instance)
	{
		return $this->Lectures->get_by_instance($instance);
	}

	public function test_insert($instance,$date,$start_time,$end_time,$location)
	{
		return $this->Lectures->insert($instance,$date,$start_time,$end_time,$location);
	}
	
	public function test_update($lecture,$date,$start_time,$end_time,$location)
	{
		return $this->Lectures->update($lecture,$date,$start_time,$end_time,$location);
	}
	
	public function test_delete($lecture)
	{
		return $this->Lectures->delete($lecture);
	}
	
	public function test($pdo,$instance,$date1,$start_time1,$end_time1,$location1,$date2,$start_time2,$end_time2,$location2)
	{
		$this->Lectures = new Lectures(); $this->Lectures->pdo = $pdo;
	
		echo "Kokeillaan muokata luentoja arvoilla<ul>";
		echo "<li>instance = " . $instance . "</li>";
		echo "<li>date = " . $date1 . " ja " . $date2 . "</li>";
		echo "<li>start_time = " . $start_time1 . " ja " . $start_time2 . "</li>";
		echo "<li>end_time = " . $end_time1 . " ja " . $end_time2 . "</li>";
		echo "<li>location = " . $location1 . " ja " . $location2 . "</li>";
		echo "</ul><ol>";
		
		if($this->test_insert($instance,$date1,$start_time1,$end_time1,$location1))
			echo "<li>Lisäys onnistui!</li>";
		
		$lectures = $this->test_get_by_instance($instance);		
		$id = null;	
		foreach($lectures as &$lecture)
			if($lecture->date       == strtotime($date1 . " " . $start_time1)
			&& $lecture->start_time == strtotime($date1 . " " . $start_time1)
			&& $lecture->end_time   == strtotime($date1 . " " . $end_time1)
			&& $lecture->location   == $location1)
			{
				$id = $lecture->id;
				break;
			}
		if($id != null)
			echo "<li>Lisätty luento löytyi!</li>";
		
		if($this->test_update($id,$date2,$start_time2,$end_time2,$location2))
			echo "<li>Muokkaus onnistui!</li>";
		
		$lectures = $this->test_get_by_instance($instance);
		$id = null;	
		foreach($lectures as &$lecture)
			if($lecture->date       == strtotime($date2 . " " . $start_time2)
			&& $lecture->start_time == strtotime($date2 . " " . $start_time2)
			&& $lecture->end_time   == strtotime($date2 . " " . $end_time2)
			&& $lecture->location   == $location2)
			{
				$id = $lecture->id;
				break;
			}
		if($id != null)
			echo "<li>Muokattu luento löytyi!</li>";
		
		if($this->test_delete($id))
			echo "<li>Poisto onnistui!</li>";
		
		$lectures = $this->test_get_by_instance($instance);
		$id = null;
		foreach($lectures as &$lecture)
			if($lecture->date       == strtotime($date2 . " " . $start_time2)
			&& $lecture->start_time == strtotime($date2 . " " . $start_time2)
			&& $lecture->end_time   == strtotime($date2 . " " . $end_time2)
			&& $lecture->location   == $location2)
			{
				$id = $lecture->id;
				break;
			}
		if($id == null)
			echo "<li>Poistettu luento hävisi taulukosta!</li>";
		
		echo "</ol>";
	}
}

?>
